package com.flying.planewar;



import com.flying.planewar.utils.PlaneUtil;

import java.awt.*;
import java.lang.invoke.VarHandle;
import java.util.Random;

/**
 * @author 刘鹏飞
 * @Classname Shell
 * @Date 2024/12/17 21:57
 * @Created by 刘鹏飞
 * @Description 炮弹类
 */
public class Shell  extends GameObject{
    // 飞行角度
    double degree;
    Color[] colors={Color.CYAN,Color.ORANGE,Color.pink,Color.WHITE,Color.BLUE};
    int i=-1;
    Random random=new Random();
    public Shell(){
        degree = Math.random()*Math.PI*2;
        x = 250;
        y = 250;
        width = 5;
        height = 5;
        speed =3;
    }

    public void drawMyself(Graphics g) {


        Color color = g.getColor();
        if (i==-1){
            // 随机设置颜色
            i= random.nextInt(colors.length );
        }
        g.setColor(colors[i]);
        g.fillOval((int) x, (int) y,this.width, this.height);




        // 随机运动任意角度
        //炮弹沿着任意角度飞行
        x += (int)(speed*Math.cos(degree));
        y += (int)(speed*Math.sin(degree));


        //如下代码，用来实现碰到边界，炮弹反弹回来(原理和打台球游戏一样)
        if(y > PlaneUtil.FRAME_HEIGHT - height-this.height || y < 25+this.height){
            degree = -degree;
        }
        if(x<this.width||x>PlaneUtil.FRAME_WIDTH-width-this.width){
            degree = Math.PI-degree;
        }

        //返回给外部，变回以前的颜色
        g.setColor(color);

    }
}
